﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AnimaGestionPersonnage.Utilities;
using AnimaGestionPersonnage.Properties;
using System.Collections;

namespace AnimaGestionPersonnage.Vue
{
    public partial class FormEditSkills : Form
    {
        private Boolean saved = true;
        
        public FormEditSkills()
        {
            InitializeComponent();
        }

        public void ShowDialog(Form p)
        {
            comboBoxTypeSkill.Items.AddRange(new ArrayList( Settings.Default.AllSkillTypes).ToArray());
            comboBoxSkillStat.Items.AddRange(new ArrayList(Settings.Default.StatTypes).ToArray());
            comboBoxSkillSet.Items.AddRange(new ArrayList(Settings.Default.SkillSet).ToArray());
            
            comboBoxSkillStat.SelectedIndex = 0;
            comboBoxTypeSkill.SelectedIndex = 0;
            comboBoxSkillSet.SelectedIndex = 0;

            base.ShowDialog(p);
        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            refreshListSkills();
        }

        private void quitterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void sauvergarderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult frmclose = DialogResult.OK;

            if (frmclose == DialogResult.OK)
            {
                Utils.xmlSerializeAllSkills();
                saved = true;
                sauvergarderToolStripMenuItem.Enabled = false;
            }
        }

        /// <summary>
        /// verifie si les donnees ont ete sauvegarde et si oui(ou qu'on force la fermeture) affiche la fenetre parent
        /// </summary>
        /// <param name="e">evenement de fermeture de la fenetre</param>
        private void FormEditSkills_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = Utils.onCloseEvent(saved, this,null,e);
        }

        private void buttonAddSkill_Click(object sender, EventArgs e)
        {
            Boolean notFound = true;

            if (textBoxSkillName.Text != "")
            {
                switch ((SkillSet)comboBoxSkillSet.SelectedIndex)
                {
                    case SkillSet.Other:
                        BasicSkill basicTempSkill = new BasicSkill();

                        basicTempSkill.name = textBoxSkillName.Text;
                        basicTempSkill.type = (TypeSkill)comboBoxTypeSkill.SelectedIndex;
                        basicTempSkill.skillSet = (SkillSet)comboBoxSkillSet.SelectedIndex;
                        foreach (BasicSkill s in Utils.basicSkillsList)
                        {
                            if (s.Equals(basicTempSkill))
                            {
                                notFound = false;
                                break;
                            }
                        }
                        if (notFound)
                        {
                            saved = false;
                            sauvergarderToolStripMenuItem.Enabled = true;
                            Utils.basicSkillsList.Add(basicTempSkill);
                        }
                        else
                        {
                            MessageBox.Show("Cette compétence existe déjà.", "Erreur",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                        }
                        break;
                    case SkillSet.Primary:
                        PrimarySkill primTempSkill = new PrimarySkill();

                        primTempSkill.name = textBoxSkillName.Text;
                        primTempSkill.type = (TypeSkill)comboBoxTypeSkill.SelectedIndex;
                        primTempSkill.stat = (Stats)comboBoxSkillStat.SelectedIndex;
                        primTempSkill.skillSet = (SkillSet)comboBoxSkillSet.SelectedIndex;
                        foreach (PrimarySkill s in Utils.primarySkillsList)
                        {
                            if (s.Equals(primTempSkill))
                            {
                                notFound = false;
                                break;
                            }
                        }
                        if (notFound)
                        {
                            saved = false;
                            sauvergarderToolStripMenuItem.Enabled = true;
                            Utils.primarySkillsList.Add(primTempSkill);
                        }
                        else
                        {
                            MessageBox.Show("Cette compétence existe déjà.", "Erreur",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                        }
                        break;
                    case SkillSet.Secondary:
                        SecondarySkill secTempSkill = new SecondarySkill();

                        secTempSkill.name = textBoxSkillName.Text;
                        secTempSkill.type = (TypeSkill)comboBoxTypeSkill.SelectedIndex;
                        secTempSkill.stat = (Stats)comboBoxSkillStat.SelectedIndex;
                        secTempSkill.skillSet = (SkillSet)comboBoxSkillSet.SelectedIndex;
                        foreach (SecondarySkill s in Utils.secondarySkillsList)
                        {
                            if (s.Equals(secTempSkill))
                            {
                                notFound = false;
                                break;
                            }
                        }
                        if (notFound)
                        {
                            saved = false;
                            sauvergarderToolStripMenuItem.Enabled = true;
                            Utils.secondarySkillsList.Add(secTempSkill);
                        }
                        else
                        {
                            MessageBox.Show("Cette compétence existe déjà.", "Erreur",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Error);
                        }
                        break;
                }
                refreshListSkills();
            }
            else
            {
                MessageBox.Show("Il manque des données.", "Erreur",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
            }
        }

        private void buttonRemSkill_Click(object sender, EventArgs e)
        {
            if (listBoxSkills.SelectedItem != null)
            {
                bool notFound = true;
                string[] tempName = ((String)listBoxSkills.SelectedItem).Split(new Char[] { '-' });

                foreach (SecondarySkill s in Utils.secondarySkillsList)
                {
                    if (s.name == tempName[0])
                    {
                        Utils.secondarySkillsList.Remove(s);
                        saved = false;
                        sauvergarderToolStripMenuItem.Enabled = true;
                        notFound = false;
                        break;
                    }
                }
                if (notFound)
                {
                    foreach (PrimarySkill s in Utils.primarySkillsList)
                    {
                        if (s.name == tempName[0])
                        {
                            Utils.primarySkillsList.Remove(s);
                            saved = false;
                            sauvergarderToolStripMenuItem.Enabled = true;
                            notFound = false;
                            break;
                        }
                    }
                    if (notFound)
                    {
                        foreach (BasicSkill s in Utils.basicSkillsList)
                        {
                            if (s.name == tempName[0])
                            {
                                Utils.basicSkillsList.Remove(s);
                                saved = false;
                                sauvergarderToolStripMenuItem.Enabled = true;
                                notFound = false;
                                break;
                            }
                        }
                    }
                }
                if (!notFound)
                {
                    refreshListSkills();
                }
            }
            else
            {
                MessageBox.Show("Aucun élément sélectioné.", "Erreur",
                                       MessageBoxButtons.OK,
                                       MessageBoxIcon.Error);
            }
        }

        private void buttonReplace_Click(object sender, EventArgs e)
        {
            buttonRemSkill_Click(null, null);
            buttonAddSkill_Click(null, null);
        }

        private void listBoxSkills_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxSkills.SelectedItem != null)
            {
                TypeSkill tempType;
                Stats tempStat;
                SkillSet tempSet;
                
                string[] tempName = ((String)listBoxSkills.SelectedItem).Split(new Char[] { '-' });
                
                textBoxSkillName.Text = tempName[0];

                tempType = Utils.getTypeSkillFromString(tempName[1], this);
                if (tempType != TypeSkill.Erreur)
                {
                    comboBoxTypeSkill.SelectedIndex = (int)tempType;
                }
                else
                {
                    comboBoxTypeSkill.SelectedIndex = 0;
                }

                tempStat = Utils.getStatFromString(tempName[2], this);
                if (tempStat != Stats.Erreur)
                {
                    comboBoxSkillStat.SelectedIndex = (int)tempStat;
                }
                else
                {
                    comboBoxSkillStat.SelectedIndex = 0;
                }

                tempSet = Utils.getSkillSetFromString(tempName[3], this);
                if (tempSet != SkillSet.Erreur)
                {
                    comboBoxSkillSet.SelectedIndex = (int)tempSet;
                }
                else
                {
                    comboBoxSkillSet.SelectedIndex = 0;
                }
            }
        }
        
        void refreshListSkills()
        {
            sortSkillLists();
            listBoxSkills.Items.Clear();
            foreach (BasicSkill s in Utils.basicSkillsList)
            {
                listBoxSkills.Items.Add(s.name + "-" + s.type + "- -" + s.skillSet);
            }
            foreach (PrimarySkill s in Utils.primarySkillsList)
            {
                listBoxSkills.Items.Add(s.name + "-" + s.type + "-" + s.stat + "-" + s.skillSet);
            }
            foreach (SecondarySkill s in Utils.secondarySkillsList)
            {
                listBoxSkills.Items.Add(s.name + "-" + s.type + "-" + s.stat + "-" + s.skillSet);
            }
        }

        void sortSkillLists()
        {
            Utils.basicSkillsList.Sort();
            Utils.primarySkillsList.Sort();
            Utils.secondarySkillsList.Sort();
        }
    }
}
